﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DeepZoomPivotConstructor.VisualLib;

namespace IndicoRender
{
    /// <summary>
    /// Some utilities to help rendering a meeting
    /// </summary>
    class IndicoRenderUtilities
    {
        /// <summary>
        /// return a string that represents when the thing starts, and its duration
        /// </summary>
        /// <param name="start"></param>
        /// <param name="finish"></param>
        /// <returns></returns>
        public static string TimeSpanToString (DateTime start, DateTime finish)
        {
            return start.ToLongDateString() + " - " + GenerateDeltaTimeString(start, finish);
        }

        /// <summary>
        /// Given a start and end time, generate a nice delta string.
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private static string GenerateDeltaTimeString(DateTime startDate, DateTime endDate)
        {
            StringBuilder bld = new StringBuilder();
            bld.AppendFormat("{0} ", startDate.ToShortTimeString());

            TimeSpan sp = endDate - startDate;
            int minutes = ((sp.Days * 24) + sp.Hours) * 60 + sp.Minutes;
            if (sp.Days > 0)
            {
                decimal days = minutes / ((decimal)(60 * 24));
                bld.AppendFormat("({0:F1} days)", days);
            }
            else if (minutes > 60)
            {
                decimal hours = minutes / ((decimal)60);
                bld.AppendFormat("({0:F1} hours)", hours);
            }
            else
            {
                bld.AppendFormat("({0:F0} minutes)", minutes);
            }
            return bld.ToString();
        }

        /// <summary>
        /// Resize an image if it is too big. Some of these conferences can get really really big!
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static Visual getSizeRight(Visual v)
        {
            var bb = v.VisualBoundingBox;
            double max = Math.Max(bb.Width, bb.Height);
            const double rescaleTo = 10.0F;
            if (max > rescaleTo)
                v.Scale(10.0F / max);
            return v;
        }
    }
}
